package com.devops.admin.util;

import org.yaml.snakeyaml.Yaml;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: config
 * @description: 转换工具类
 * @author: xujingyang
 * @create: 2019-03-27 12:16
 **/
public class TransferUtils {

    public static List<String> ymlString2Properties(String yamlString) {
        Yaml yaml = new Yaml();
        Map<String,Object> load = yaml.load(yamlString);
        ArrayList<String> line = new ArrayList<>();
        for (Map.Entry<String, Object> entry : load.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            StringBuffer sb = new StringBuffer();
            sb.append(entry.getKey());
            digui(sb,entry.getValue(),line);
            System.out.println(sb);
        }
        System.out.println(line.size());
        return line;
    }

    public static void digui(StringBuffer sb, Object o, List<String> line) {
        if(o instanceof LinkedHashMap){
            LinkedHashMap<String,Object> o1 = (LinkedHashMap<String,Object>) o;
            for (Map.Entry<String, Object> entry : o1.entrySet()) {
                StringBuffer sbnei = new StringBuffer();
                sbnei.append(sb.toString());
                sbnei.append("."+entry.getKey());
                digui(sbnei,entry.getValue(),line);
            }
        }else if(o instanceof List){
            ArrayList<Object> o1 = (ArrayList<Object>) o;
            for(int i=0; i<o1.size(); i++){
                StringBuffer sbnei = new StringBuffer();
                sbnei.append(sb.toString());
                sbnei.append("\\["+i+"\\]");
                digui(sbnei,o1.get(i),line);
            }
        }else{  //string,long,integer,boolean,char.....
            sb.append("="+o.toString());
            line.add(sb.toString());
        }
    }
}
